package bb.lanxing.device.sgdevcie.setting.fragment

import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import bb.lanxing.R
import bb.lanxing.databinding.FragmentSgDeviceSettingEntryBinding
import bb.lanxing.device.sgdevcie.viewmodel.XossDeviceSGSettingEntryViewModel
import bb.lanxing.device.sgdevcie.viewmodel.DeviceViewModel
import bb.lanxing.lib.devices.sgsettingentity.setting.CommonSettings
import bb.lanxing.mvvm.base.BaseDBFragment
import kotlinx.coroutines.launch
import java.io.File

class SGDeviceSettingEntryFragment :
    BaseDBFragment<FragmentSgDeviceSettingEntryBinding>(R.layout.fragment_sg_device_setting_entry) {
    private val viewModel by activityViewModels<DeviceViewModel>()
    private val entryViewModel by activityViewModels<XossDeviceSGSettingEntryViewModel>()

    private var settings: CommonSettings? = null
    private var settingFile: File? = null
    override fun initView(t: FragmentSgDeviceSettingEntryBinding) {
        register()
        if (null != entryViewModel.address && null != entryViewModel.deviceName && null != entryViewModel.deviceType) {
            viewModel.init(
                entryViewModel.address, entryViewModel.deviceName, entryViewModel.deviceType
            )
        }
    }

    private fun register() {
        lifecycleScope.launch {
            viewModel.commonSettingSharedFlow.collect {
                dismissLoadingDialog()
                settings = it
            }
        }

    }

    fun getSettings(): CommonSettings? {
        return settings
    }

    fun setSettings(commonSettings: CommonSettings?) {
        settings = commonSettings
    }

    fun getSettingFile(): File? {
        return this.settingFile
    }

    fun setSettingFile(file: File?) {
        settingFile = file
    }
}